//
//  ReactorKitTestVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/5/10.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import ReactorKit

class ReactorKitTestVC: UIViewController, View {

    var disposeBag = DisposeBag()

    var refreshButton = UIButton.init().then {
        $0.setTitle("refresh", for: .normal)
        $0.setTitleColor(.white, for: .normal)
        $0.backgroundColor = .blue
    }

    var followButton = UIButton.init().then {
        $0.setTitle("follow", for: .normal)
        $0.setTitleColor(.white, for: .normal)
        $0.backgroundColor = .blue
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        view.addSubviews([refreshButton, followButton])

        refreshButton.snp.makeConstraints { make in
            make.top.equalTo(view).offset(100)
            make.left.equalTo(view).offset(20)
            make.right.equalTo(view).offset(-20)
            make.height.equalTo(44)
        }

        followButton.snp.makeConstraints { make in
            make.top.equalTo(refreshButton.snp_bottomMargin).offset(20)
            make.left.equalTo(view).offset(20)
            make.right.equalTo(view).offset(-20)
            make.height.equalTo(44)
        }
        // Do any additional setup after loading the view.
    }


    func bind(reactor: ProfileViewReactor) {
      // action (View -> Reactor)
      refreshButton.rx.tap.map { Reactor.Action.refresh }
        .bind(to: reactor.action)
        .disposed(by: self.disposeBag)

      // state (Reactor -> View)
      reactor.state.map { "\($0.isFollowing)" }
        .bind(to: followButton.rx.title(for: .normal))
        .disposed(by: self.disposeBag)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
